#!usr/bin/env python  
# -*- coding:utf-8 _*-
""" 
@author:robot
@file: V1Task.py 
@version:
@time: 2022/03/10 
"""


class V2DataFlow:

    def __init__(self, name):
        self.name = name
        from TapClientCore.T03_api_body_templates.v2_api_body import DATAFLOW_SETTING
        self.setting = DATAFLOW_SETTING.copy()
        self.setting['name'] = name
        from TapClientCore.T04_logic.DagforDFS.Dag.V2Dag import V2Dag
        self.dag = V2Dag()
        self.stage = None

    def _clone(self, stage):
        self.stage = stage
        return self

    def config(self, setting):
        self.setting.update(setting)

    def readFrom(self, source):
        # syncPoints update
        from TapClientCore.T03_api_body_templates.v2_api_body import DATAFLOW_SYNC_POINTS
        tmp = DATAFLOW_SYNC_POINTS.copy()
        source_id = source.node.get('connectionId')
        tmp.update({'connectionId': source_id})
        self.setting['syncPoints'] = [tmp]
        return self._clone(source)

    def writeTo(self, sink, relation):
        self.dag.edge(self.stage, sink, relation)

    @property
    def dataflow(self):
        attr = {
            'name': self.name,
            'stages': self.dag.stages,
            'dataSourceModel': self.dag.data_source_model,
            'setting': self.setting
        }
        return attr


if __name__ == '__main__':
    from pprint import pprint
    from TapClientCore.T04_logic.DagforDFS.node.data_copy_source_node import DataCopySourceNode
    from TapClientCore.T04_logic.DagforDFS.node.data_copy_target_node import DataCopySinkNode
    from TapClientCore.T04_logic.DagforDFS.relations.relation import V2DataFlowMapTable

    S = DataCopySourceNode('auto_mysql')
    T = DataCopySinkNode('auto_mysql')
    d = V2DataFlow('test')
    d.config({})
    d.readFrom(S).writeTo(T, V2DataFlowMapTable({'syncObjects': {"objectNames": ["my2"], "type": "table"}}))
    from pprint import pprint

    pprint(d.dataflow)
